package com.lingang.framework.web.domain;

import com.lingang.framework.web.enums.ApiRequestStatusEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @param <T>
 * @author zhangkaicheng
 */
@ApiModel(description = "")
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    @ApiModelProperty(hidden = true)
    private static final long serialVersionUID = 4111085789958984329L;
    /**
     * code
     */
    @ApiModelProperty("code")
    private Integer code;
    /**
     * 信息
     */
    @ApiModelProperty("信息")
    private String message;
    /**
     * data
     */
    @ApiModelProperty("data")
    private T data;

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(ApiRequestStatusEnum statusEnum) {
        this.code = statusEnum.getCode();
        this.message = statusEnum.getMessage();
    }

    public Result(ApiRequestStatusEnum statusEnum, String message) {
        this.code = statusEnum.getCode();
        this.message = message;
    }

    public Result(ApiRequestStatusEnum statusEnum, T data) {
        this.code = statusEnum.getCode();
        this.data = data;
    }

    public Result(ApiRequestStatusEnum statusEnum, String message, T data) {
        this.code = statusEnum.getCode();
        this.message = message;
        this.data = data;
    }

    public void setStatus(ApiRequestStatusEnum statusEnum) {
        this.code = statusEnum.getCode();
        if (this.message == null) {
            this.message = statusEnum.getMessage();
        }
    }

    /**
     * @return code == 200
     */
    public boolean success() {
        return Objects.equals(ApiRequestStatusEnum.SUCCESS.getCode(), this.code);
    }
}
